package com.Buffered;

import java.io.*;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/4/8 11:04
 * @description
 */
public class BufferedDemo {
//    bufferedInputstream
    static String s3 = "d:\\file\\test\\0003.txt";
    static String s1 = "d:\\file\\test\\0001.txt";
    public static void bisDemo(){
        try {
            InputStream is = new FileInputStream("d:\\file\\test\\0001.txt");
            BufferedInputStream bis = new BufferedInputStream(is);

            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream("d:\\file\\test\\0003.txt")
            );
            byte[] buff = new byte[1024];
            int len = 0;
            while ((len=bis.read(buff))!=-1){
                bos.write(buff,0,len);
                //带有buffer的必须要带强制刷新缓冲区
                bos.flush();
            }
            bos.close();
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //bufferedReader
    public static void brw(){
        try {
            FileReader reader = new FileReader(s1);
            BufferedReader br = new BufferedReader(reader);
            BufferedWriter bw = new BufferedWriter(
                    new FileWriter(s3)
            );
            char[] chars = new char[1024];
            int len = 0;
            while ((len = br.read(chars))!=-1){
                bw.write(chars,0,len);
                bw.newLine();
                bw.flush();
            }
            bw.close();
            br.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //InputStreamReader 转换流。将字节流转换成字符流
    //传入的是字节流 传出来是字符流
    public static void isrDemo(){
        try {
            //通过字节流读取
            InputStreamReader isr = new InputStreamReader(
                    new FileInputStream(s1));
            //转换成字符流
            BufferedReader br = new BufferedReader(isr);
            char[] chars = new char[256];
            int len = 0;
            while ((len = br.read(chars))!=-1){
                System.out.println(new String(chars,0,len));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        bisDemo();
        brw();


    }
}
